import { Injectable } from '@angular/core';
// tslint:disable-next-line:import-blacklist
import { Observable } from 'rxjs';

@Injectable()
export class WebSocketService {

    ws: WebSocket;
    ws2: WebSocket;
    ws3: WebSocket;
    ws4: WebSocket;
    ws5: WebSocket;
    ws6: WebSocket;
    ws_phev: WebSocket;
    ws_bev: WebSocket;

    constructor() {
    }

    createObservableSocket(url: string): Observable<any> {
        this.ws = new WebSocket(url);

        // if(this.ws){this.ws.close()}

        return new Observable(
            observer => {
                // 什么时候发生下一个元素
                this.ws.onmessage = (event) => observer.next(event.data);
                // 什么时候抛一个异常
                this.ws.onerror = (event) => observer.error(event);
                // 什么时候发出流结束的信号
                this.ws.onclose = (event) => observer.complete();

            }
        );
    }

    sendMessage(message: string) {
        this.ws.send(message);
    }


    createObservableSocket2(url: string): Observable<any> {
        this.ws2 = new WebSocket(url);

        // if(this.ws){this.ws.close()}

        return new Observable(
            observer => {
                // 什么时候发生下一个元素
                this.ws2.onmessage = (event) => observer.next(event.data);
                // 什么时候抛一个异常
                this.ws2.onerror = (event) => observer.error(event);
                // 什么时候发出流结束的信号
                this.ws2.onclose = (event) => observer.complete();

            }
        );
    }

    sendMessage2(message: string) {
        this.ws2.send(message);
    }

    createObservableSocket3(url: string): Observable<any> {
        this.ws3 = new WebSocket(url);

        // if(this.ws){this.ws.close()}

        return new Observable(
            observer => {
                // 什么时候发生下一个元素
                this.ws3.onmessage = (event) => observer.next(event.data);
                // 什么时候抛一个异常
                this.ws3.onerror = (event) => observer.error(event);
                // 什么时候发出流结束的信号
                this.ws3.onclose = (event) => observer.complete();

            }
        );
    }

    sendMessage3(message: string) {
        this.ws3.send(message);
    }

    createObservableSocket4(url: string): Observable<any> {
        this.ws4 = new WebSocket(url);

        // if(this.ws){this.ws.close()}

        return new Observable(
            observer => {
                // 什么时候发生下一个元素
                this.ws4.onmessage = (event) => observer.next(event.data);
                // 什么时候抛一个异常
                this.ws4.onerror = (event) => observer.error(event);
                // 什么时候发出流结束的信号
                this.ws4.onclose = (event) => observer.complete();

            }
        );
    }

    sendMessage4(message: string) {
        this.ws4.send(message);
    }

    createObservableSocket5(url: string): Observable<any> {
        this.ws5 = new WebSocket(url);

        // if(this.ws){this.ws.close()}

        return new Observable(
            observer => {
                // 什么时候发生下一个元素
                this.ws5.onmessage = (event) => observer.next(event.data);
                // 什么时候抛一个异常
                this.ws5.onerror = (event) => observer.error(event);
                // 什么时候发出流结束的信号
                this.ws5.onclose = (event) => observer.complete();

            }
        );
    }

    sendMessage5(message: string) {
        this.ws5.send(message);
    }

    createObservableSocket6(url: string): Observable<any> {
        this.ws6 = new WebSocket(url);

        // if(this.ws){this.ws.close()}

        return new Observable(
            observer => {
                // 什么时候发生下一个元素
                this.ws6.onmessage = (event) => observer.next(event.data);
                // 什么时候抛一个异常
                this.ws6.onerror = (event) => observer.error(event);
                // 什么时候发出流结束的信号
                this.ws6.onclose = (event) => observer.complete();

            }
        );
    }

    sendMessage6(message: string) {
        this.ws6.send(message);
    }
    // phev
    createObservableSocket_phev(url: string): Observable<any> {
        this.ws_phev = new WebSocket(url);
        return new Observable(
            observer => {
                // 什么时候发生下一个元素
                this.ws_phev.onmessage = (event) => observer.next(event.data);
                // 什么时候抛一个异常
                this.ws_phev.onerror = (event) => observer.error(event);
                // 什么时候发出流结束的信号
                this.ws_phev.onclose = (event) => observer.complete();

            }
        );
    }
    sendMessage_phev(message: string) {
        this.ws_phev.send(message);
    }
    // bev
    createObservableSocket_bev(url: string): Observable<any> {
        this.ws_bev = new WebSocket(url);
        return new Observable(
            observer => {
                // 什么时候发生下一个元素
                this.ws_bev.onmessage = (event) => observer.next(event.data);
                // 什么时候抛一个异常
                this.ws_bev.onerror = (event) => observer.error(event);
                // 什么时候发出流结束的信号
                this.ws_bev.onclose = (event) => observer.complete();

            }
        );
    }
    sendMessage_bev(message: string) {
        this.ws_bev.send(message);
    }   
}

